package com.jiwei.system.controller;


import com.jiwei.system.pojo.Member;
import com.jiwei.system.pojo.MemberLogin;
import com.jiwei.system.pojo.ResultPoJo;
import com.jiwei.system.serviceImpl.PwdLoginService;
import com.jiwei.system.utils.JWTUtil;
import com.jiwei.system.utils.CookieUtil;
import com.jiwei.system.serviceImpl.MemberServiceImpl;
import com.jiwei.system.vo.Login;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

@RequestMapping(value = {"member", "operator", "admin"}, method = RequestMethod.POST)
@Validated
@RestController
public class MemberActionController {

    @Resource(name = "MemberService")
    private MemberServiceImpl memberService;

    @Resource(name = "Pwd")
    private PwdLoginService pwdService;

    @PostMapping("login")
    public String loginVerify(@RequestBody MemberLogin memberLogin, HttpServletResponse response,HttpServletRequest request){
        String account = memberLogin.getAccount();
        String password = memberLogin.getPasswordMD5();
        Login newlogin = new Login(account,password);
        memberLogin = newlogin.changeToPo();
        ResultPoJo loginresult = pwdService.verifyLogin(memberLogin);
        System.out.println(memberLogin);
        if(loginresult.getStatusCode()=="200"){
            System.out.println("成员登录信息验证成功");
            String role = memberService.getMemberRoleByID(account);
            System.out.print("获取的登录身份为");
            System.out.println(role);
            memberLogin.setRole(role);
            request.getSession().setAttribute("login",memberLogin);
            System.out.println("写入session成功");
//            String token = JWTUtil.createLoginToken(account);
//            CookieUtil.setMemberCookie(response,token);

//            String roleToken = JWTUtil.createRoleToken(role);
//            CookieUtil.setRoleCookie(response,roleToken);
            memberLogin = (MemberLogin) request.getSession().getAttribute("login");
//            String ac = (String) request.getSession().getAttribute("account");
//            String ro = (String) request.getSession().getAttribute("role");
            String ac = memberLogin.getAccount();
            System.out.print("从seesion中获取的对象为");
            System.out.println(memberLogin);
            System.out.print("账号为");
            System.out.println(ac);


            return "{" +
                    "\"statusCode\":\"" + 200 + '\"' +
                    ", \"message\":\"" + "登录成功"  + '\"' +
                    ", \"role\":\"" + role + "\""+
                    '}';
        }

        return "{" +
                "\"statusCode\":\"" + 400 + '\"' +
                ", \"message\":\"" + "登录失败" + "\""+
                '}';
    }

    @PostMapping("index")
    public String loginindex(HttpServletResponse response, HttpServletRequest request,@RequestParam(value = "account")  String account, @RequestParam(value = "role") String role){
        MemberLogin mem = (MemberLogin) request.getSession().getAttribute("login");
//        String token = JWTUtil.createLoginToken(account);
//        CookieUtil.setMemberCookie(response,token);
//        System.out.print("主页写token");
//        System.out.println(token);
//        String roleToken = JWTUtil.createRoleToken(role);
//        System.out.print("主页写roletoken");
//        System.out.println(roleToken);
//        CookieUtil.setRoleCookie(response,roleToken);
            return "{" +
                    "\"statusCode\":\"" + 200 + '\"' +
                    ", \"message\":\"" + "写入token成功"  + '\"' +
                    '}';


    }

    /**
     * 获取个人信息页
     * @param request 获取前端内容的设置
     * @return member对象
     */
    @PostMapping("getMyInfo")
    public Member getMyInfo(HttpServletRequest request){
        MemberLogin memberlogin = (MemberLogin) request.getSession().getAttribute("login");
//        String token = CookieUtil.getCookieValueNamedToken(request);
        String account = memberlogin.getAccount();
        return memberService.getMyInfo(account);
    }

    /**
     * 更新个人信息
     * @param member 将接受的信息通过注解反射成功member对象
     * @param request 用来获取cookie
     * @return 没有意义只是为了告诉前端成功了，但可以有很多种实现方式
     */
    @PostMapping("updateMyInfo")
    public String updateMyInfo(@RequestBody Member member,HttpServletRequest request){
//        String token = CookieUtil.getCookieValueNamedToken(request);
//        String account = JWTUtil.getClaimNamedAccount(token);
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String account = memberLogin.getAccount();
        member.setAccount(account);
        memberService.updatePersonalInfo(member);
        return "{" +
                "\"statusCode\":\"" + 200 + '\"' +
                ", \"message\":\"" + "更新成功" + "\""+
                '}';
    }

    /**
     * 修改密码，修改前会做密码认证
     * @param response 用于移除cookie
     * @param request  用于获取cookie
     * @param password 新密码
     * @param newPassword 旧密码
     * @return 如果旧密码正确则成功修改密码,删除登录凭证返回true 否则返回false
     */
    @PostMapping("changePassword")
    public String changePassword(HttpServletResponse response, HttpServletRequest request, @RequestParam(value = "password") @NotBlank String password, @RequestParam(value = "newPassword") @NotBlank String newPassword){
        MemberLogin memberLogin = (MemberLogin) request.getSession().getAttribute("login");
        String account = memberLogin.getAccount();
        if(memberService.changePassword(account,password,newPassword)){
            request.getSession().removeAttribute("login");
            return "{" +
                    "\"statusCode\":\"" + 200 + '\"' +
                    ", \"message\":\"" + "密码修改成功" + "\""+
                    '}';
        }
        return "{" +
                "\"statusCode\":\"" + 400 + '\"' +
                ", \"message\":\"" + "密码修改失败" + "\""+
                '}';
    }


}
